<?php
namespace app\admin\controller;
use think\Controller;
use app\common\model\AdminInfo;

class Index extends Controller
{

    public function login()
    {
          return $this->fetch();
    }
    public function signin()
    {
          $post = input('post.');
          $adminInfo = new AdminInfo;
          $_opt['admin_accounts'] = trim(input('post.admin_accounts'));
          $_opt['delete'] = 0;
          $isfind = $adminInfo->getUserInfo($_opt);
          if(!empty($isfind)){
            $adminPW = md5($isfind['salt'].trim(input('post.admin_password')));
            if($isfind['admin_status']==2){
                return $this->error('该用户没有权限');
            }
            if( $isfind['admin_password'] ===  $adminPW){
                session('admin_id',$isfind['admin_id']);
                session('admin_user',$isfind['admin_accounts']);
                session('admin_login_date',date("Y-m-d H:i:s"));
                //保存登录时间
//                $this->saveLoginTime($isfind['id']);
                //登录来源页面
                // if(session('redirect_url')){
                //     return redirect()->restore();
                // }else{
                //     return $this->redirect('Index/index');
                // }
                 return $this->ajaxSuccess('登录成功');

            }else{
                //错误次数
                $error_times = session('login_error_times');
                $error_times ++;
                session('login_error_times',$error_times);

                //return $this->error('密码错误');
                return $this->ajaxError('密码错误');
            }
        }else{
            //错误次数
            $error_times = session('login_error_times');
            $error_times ++;
            session('login_error_times',$error_times);
            //return $this->error('没有此用户,或用户已被禁用');
            return $this->ajaxError('没有此用户,或用户已被禁用');
        }

    }

     public function ajaxSuccess($msg='成功', array $data=[]){
        if(!empty($data)){
            $data['message'] = $msg;
            return json($data);
        }
        return json(['status'=>0,'message'=>$msg]);
    }

    public function ajaxError($msg='失败'){
        return json(['status'=>-1,'message'=>$msg]);
    }

    public function showError($msg, $url=''){
        if($this->isAjax){
            $data = ['message' => $msg,];
            return json($data);
        }
    }


}
